module EStep {
    export class DashCtrl {
        public static $inject = ['PluginService', 'Socket', '$moment'];
        public networkState;
        public socketInfo     = {
            server: '192.168.1.160:55555',
            state:  0,
            data:   []
        };

        constructor(private PluginService: PluginService, private Socket: Socket, private $moment) {
            this.networkState = PluginService.networkState;
            const base        = this;
            Socket.addMessageHandler((message) => {
                base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + message);
                console.log(base.$moment().format('HH:mm:ss') + ' ' + message);
            });
            Socket.setStatusChangeCallback((socketStatus: number) => {
                base.socketInfo.state = socketStatus;
                base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + ' State Change:' + socketStatus);
            });
            // $websocket.onMessage((message) => {
            //     base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + ' ' + message.data);
            //     console.log(message);
            // }, {autoApply: true});
            //
            // this.$websocket.onOpen((event: Event) => {
            //     base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + ' onOpen.');
            // });
            //
            // this.$websocket.onClose((event: CloseEvent) => {
            //     base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + ' onClose.');
            // });
            //
            // this.$websocket.onError((event: Event) => {
            //     base.socketInfo.data.push(base.$moment().format('HH:mm:ss') + ' onError.');
            //     console.log(angular.toJson(event));
            // });
        }

        public startBackground(): void {
            this.PluginService.EnableBackground();
        }

        public endBackground(): void {
            this.PluginService.DisableBackground();
        }

        public link(): void {
            if (this.socketInfo.server == '') {
                return;
            }
            this.socketInfo.data.length = 0;
            this.Socket.setSocketUrl(this.socketInfo.server);
            this.Socket.connect();
        }
    }

    export class ChatsCtrl {
        public static $inject = ['Chats'];
        public chats;

        constructor(private Chats: Chats) {
            this.chats = Chats.all();
        }

        public remove(chat) {
            this.Chats.remove(chat);
        }

    }

    export class ChatDetailCtrl {
        public static $inject = ['$stateParams', 'Chats'];
        public chat;

        constructor($stateParams, Chats) {
            this.chat = Chats.get($stateParams.chatId);
        }
    }

    export class AccountCtrl {
        public settings = {
            enableFriends: true
        };
    }

    export class Chats {
        private chats = [{
            id:       0,
            name:     'Ben Sparrow',
            lastText: 'You on your way?',
            face:     'img/ben.png'
        }, {
            id:       1,
            name:     'Max Lynx',
            lastText: 'Hey, it\'s me',
            face:     'img/max.png'
        }, {
            id:       2,
            name:     'Adam Bradleyson',
            lastText: 'I should buy a boat',
            face:     'img/adam.jpg'
        }, {
            id:       3,
            name:     'Perry Governor',
            lastText: 'Look at my mukluks!',
            face:     'img/perry.png'
        }, {
            id:       4,
            name:     'Mike Harrington',
            lastText: 'This is wicked good ice cream.',
            face:     'img/mike.png'
        }];

        public all() {
            return this.chats;
        }

        public remove(chat) {
            this.chats.splice(this.chats.indexOf(chat), 1);
        }

        public get(chatId) {
            for (let i = 0; i < this.chats.length; i++) {
                if (this.chats[i].id === parseInt(chatId)) {
                    return this.chats[i];
                }
            }
            return null;
        }

    }

    angular.module('EStep')
        .controller('DashCtrl', DashCtrl)
        .controller('ChatsCtrl', ChatsCtrl)
        .controller('ChatDetailCtrl', ChatDetailCtrl)
        .controller('AccountCtrl', AccountCtrl)
        .service('Chats', Chats);
}